const ap = new APlayer({
    container: document.getElementById('aplayer'),
    fixed: true,
    listMaxHeight: 90,
    lrcType: 1,
    audio: []
});

// 歌曲列表
let playList = [];

// 正在播放时的歌曲
let play = {}
// 播放时 Id
let playId = 0

// 获取音频播放列表
fetch('https://framework.mzshe.cn/open/get_wy_music?key=洛天依，乐正绫', {
    method: 'get'
}).then(res => {
    return res.json()
}).then(res => {
    for (const resultKey in res.result.songs) {
        playList.push({
            name: res.result.songs[resultKey].name,
            artist: res.result.songs[resultKey].ar.map(e => e.name).toString(),
            url: '',
            cover: res.result.songs[resultKey].al.picUrl,
            lrc: '[00:00.00]暂无歌词',
            id: res.result.songs[resultKey].id
        })
    }
    play = playList.length > 0 ? playList[0] : play
    getFilePath()
})

// 获取音频播放地址
const getFilePath = function () {
    fetch(`https://framework.mzshe.cn/open/get_wy_music_url?id=${play.id}`, {
        method: 'get'
    }).then(res => {
        return res.json()
    }).then(url => {
        play.url = url.items.url
        playList[playId] = url.items.url
        ap.list.add(play)
        ap.list.switch(playId);
    })
}

// 下一首
ap.skipForward = function () {
    if (playId === playList.length - 1) {
        play = playList[0]
        playId = 0
    } else {
        play = playList[playId += 1]
    }

    if (play.url === '') {
        getFilePath()
    } else {
        ap.list.switch(playId);
    }
}
